﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace Common
{
    public class DataPackage
    {
        private const int DATA_HEADER_SIZE = 8;

        private byte version =5;
        private short dataSourceId = 0;
        private byte channelId = 0;
        private byte[] payload = null;

        public DataPackage()
        {
        }

        public DataPackage(int dataSourceId, int channelId, byte[] payload)
        {
            this.dataSourceId = (short)dataSourceId;
            this.channelId = (byte)channelId;
            this.payload = payload;
        }

        public bool IsAdmin
        {
            get
            {
                return channelId == 0;
            }
        }

        public int getDataSourceId()
        {
            return dataSourceId;
        }

        public int getChannelId()
        {
            return channelId;
        }

        public byte[] getPayload()
        {
            return payload;
        }

        public bool read(Stream dis)
        {
            try
            {

                byte[] header = new byte[DATA_HEADER_SIZE];


                if (dis.Read(header, 0, DATA_HEADER_SIZE) <= 0)
                {
                    return false;
                }
                this.version = header[0];
                if (version != 5)
                {
                    throw new IOException("version not compatible");
                }
                channelId = header[1];

                Int32 payloadLen = BitConverter.ToInt32(header, 2);
                if (BitConverter.IsLittleEndian)
                {
                    payloadLen = IPAddress.NetworkToHostOrder(payloadLen);
                }


                payload = new byte[payloadLen - DATA_HEADER_SIZE];

                dataSourceId = (short)BitConverter.ToInt16(header, 6);
                if (BitConverter.IsLittleEndian)
                {
                    dataSourceId = IPAddress.NetworkToHostOrder(dataSourceId);
                }

                int len = 0;
                while (len < payload.Length)
                {
                    len += dis.Read(payload, len, payload.Length - len);
                }

               
                string payloadString = Encoding.UTF8.GetString(payload);
                
            }
            catch (System.Exception e)
            {
                return false;
            }
            return true;
        }

        public bool write(Stream dos)
        {
            try
            {
                dos.WriteByte(version);
                dos.WriteByte(channelId);

                byte[] bytes;
                int len = payload.Length + DATA_HEADER_SIZE;
                if (BitConverter.IsLittleEndian)
                {
                    len = IPAddress.HostToNetworkOrder(len);
                }
                bytes = BitConverter.GetBytes(len);

                dos.Write(bytes, 0, 4);

                if (BitConverter.IsLittleEndian)
                {
                    dataSourceId = IPAddress.HostToNetworkOrder(dataSourceId);
                }

                bytes = BitConverter.GetBytes(dataSourceId);
                dos.Write(bytes, 0, 2);
                dos.Write(payload, 0, payload.Length);
                dos.Flush();
            }
            catch (System.Exception e)
            {
                return false;
            }
            return true;

        }



    }

}
